<include file="public/header" />
<include file="public/left" />
<style type="text/css">
    #l-map{ width:700px; height: 400px; }
</style>
<script type="text/javascript"
        src="http://api.map.baidu.com/api?v=2.0&ak=4IU3oIAMpZhfWZsMu7xzqBBAf6vMHcoa">
</script>
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表">返回上一页</a>
            <div class="subject">
                <h3>信息编辑</h3>
                <h5>网站系统权限资源管理</h5>
            </div>
        </div>
    </div>
    <div id="l-map"></div>
    <div id="r-result">请输入:<input type="text" id="suggestId" size="20" value="百度" style="width:150px;" /></div>
    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
    <form method="post" enctype="multipart/form-data">
        <section>
            <ul class="ulColumn2">
                <style>
                    .table{width: 300px;margin-left: 120px;text-align: center}
                    .textbox {
                        border: 1px #139667 solid;
                         height: 29px;
                        line-height: 26px;
                        padding: 5px;
                        font-size: 12px;
                        vertical-align: middle;
                    }
                </style>
                <li>
                    <span class="item_name" style="width:120px;">线路节点： </span><input class="link_btn" type="button" onclick="addOption()" value="增加线路节点" />
                </li>

                <li>
                    <table class="table" id="option">
                        <tr><th width="300px;">操作</th><th width="600px;">地址</th><th width="260px;">经度</th><th width="260px;">纬度</th></tr>
                        <if condition="isset($route_address)">
                            <volist name="route_address" id="v">
                                <tr>
                                    <td >
                                        <div style="width: 50px;">
                                            <a href="javascript:;" onclick="deleteParam1(this)"  style="margin-top:10px;" title="删除">删除</a>
                                            <a href="javascript:;" onclick="insertOption(this)"  style="margin-top:10px;" title="插入">插入</a>
                                        </div>
                                    </td>
                                    <td><input type="text" name="address[]" value="{$v.address}"/></td>
                                    <td><input type="text" name="lng[]" value="{$v.lng}"/></td>
                                    <td><input type="text" name="lat[]"  value="{$v.lat}"/></td>
                                    <input type="hidden" name="ids[]" value="{$v.id}" >
                                </tr>
                            </volist>

                        </if>
                        <!--<tr>-->
                            <!--<td >-->
                                <!--<div style="width: 50px;">-->
                                    <!--<a href="javascript:;" onclick="deleteParam1(this)"  style="margin-top:10px;" title="删除">删除</a>-->
                                <!--</div>-->
                            <!--</td>-->
                            <!--<td><input type="text" name="address[]" /></td>-->
                            <!--<td><input type="text" name="lng[]" /></td>-->
                            <!--<td><input type="text" name="lat[]"  /></td>-->
                            <!--<td><input type="text" name="num[]"  /></td>-->
                            <!--<input type="hidden" name="ids[]" >-->
                        <!--</tr>-->
                    </table>
                </li>
                <li>
                    <input type="hidden" name="id" value="{$id?$id:'';}">
                    <span class="item_name" style="width:120px;"></span>
                    <input type="button" class="link_btn1 link_btn" value="提交" />
                </li>
            </ul>
        </section>
    </form>
    <script>

        function addOption() {
            $.ajax({
                "url": "{:url('Set/optiontpl')}",
                success: function(data) {
                    $('#option').append(data);
                }
            });
            return;
        }
        function insertOption(o) {
            var obj = $(o).parents("tr");
            $.ajax({
                "url": "{:url('Set/optiontpl')}",
                success: function(data) {
                    obj.after(data);
                }
            });
            return;
        }

        function deleteParam1(o) {
            $(o).parents("tr").remove();

            return;
        }


        $(".link_btn1").click(function(){
            var formData = new FormData();
            var id = $('input[name="id"]').val();
//            alert(id);
            var specs1 = [];
            for(var i = 1; i<$('#option tr').length; i++){
                var optioni = $('#option tr')[i];
//                console.log(optioni);
                var address = optioni.children[1].children[0].value;
                var lng = optioni.children[2].children[0].value;
                var lat = optioni.children[3].children[0].value;
                var ids = optioni.children[4].value;
                if(lat == ''|| lng ==''||address==''){layer.alert('参数填写完整');}
                var spec = {"address": address, "lng": lng, "lat": lat, "ids":ids};
                specs1.push(spec);
            }
            var specs = JSON.stringify(specs1)
//            console.log(specs);

            formData.append('specs',specs);
            formData.append('id',id);
//            console.log(formData);

            $.ajax({
                url:"{:url('Set/do_routeedit')}",
                type:'POST',
                data:formData,
                async:false,
                processData:false,
                contentType:false,
                success:function(result){
//                    console.log(result)
                    if(result.code == 0){
                        alert(result.msg);
                    }else{
                        location.href = "{:url('set/route')}";
                    }
                }
            });
        })
    </script>
    <script type="text/javascript" src="__STATIC__/admin/js/jquery-ui-1.10.3.min.js"></script>
    <script type="text/javascript">
        // 百度地图API功能
        function G(id) {
            return document.getElementById(id);
        }

        var map = new BMap.Map("l-map");
        map.centerAndZoom("北京",11);                   // 初始化地图,设置城市和地图级别。

        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                {"input" : "suggestId"
                    ,"location" : map
                });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 18);
                alert('您的位置：'+pp.lng+','+pp.lat);
                map.addOverlay(new BMap.Marker(pp));    //添加标注
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);

        }
    </script>
</div>
</section>
</body>

</html>